| Conditions | 2 |
| Paths | 2 |
| Total Lines | 76 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | const db = require("../db/database.js"); |
||
| 32 | db.run(sql, copyApiKey, (err) => { |
||
| 33 | if (err) { |
||
| 34 | return res.status(500).json({ |
||
| 35 | errors: { |
||
| 36 | status: 500, |
||
| 37 | source: "/copy_products", |
||
| 38 | title: "Database error", |
||
| 39 | detail: err.message |
||
| 40 | } |
||
| 41 | }); |
||
| 42 | } else { |
||
|
|
|||
| 43 | let sql = "INSERT INTO orders" + |
||
| 44 | " (orderId," + |
||
| 45 | " customerName," + |
||
| 46 | " customerAddress," + |
||
| 47 | " customerZip," + |
||
| 48 | " customerCity," + |
||
| 49 | " customerCountry," + |
||
| 50 | " statusId," + |
||
| 51 | " apiKey)" + |
||
| 52 | " SELECT orderId," + |
||
| 53 | " customerName," + |
||
| 54 | " customerAddress," + |
||
| 55 | " customerZip," + |
||
| 56 | " customerCity," + |
||
| 57 | " customerCountry," + |
||
| 58 | " statusId," + |
||
| 59 | "'" + apiKey + "'" + |
||
| 60 | " FROM orders" + |
||
| 61 | " WHERE apiKey = ?"; |
||
| 62 | |||
| 63 | db.run(sql, copyApiKey, (err) => { |
||
| 64 | if (err) { |
||
| 65 | return res.status(500).json({ |
||
| 66 | errors: { |
||
| 67 | status: 500, |
||
| 68 | source: "/copy_orders", |
||
| 69 | title: "Database error", |
||
| 70 | detail: err.message |
||
| 71 | } |
||
| 72 | }); |
||
| 73 | } else { |
||
| 74 | let orderItemsSQL = "INSERT INTO order_items" + |
||
| 75 | " (orderId," + |
||
| 76 | " productId," + |
||
| 77 | " amount," + |
||
| 78 | " apiKey)" + |
||
| 79 | " SELECT orderId," + |
||
| 80 | " productId," + |
||
| 81 | " amount," + |
||
| 82 | "'" + apiKey + "'" + |
||
| 83 | " FROM order_items" + |
||
| 84 | " WHERE apiKey = ?"; |
||
| 85 | |||
| 86 | db.run(orderItemsSQL, copyApiKey, (err) => { |
||
| 87 | if (err) { |
||
| 88 | return res.status(500).json({ |
||
| 89 | errors: { |
||
| 90 | status: 500, |
||
| 91 | source: "/copy_orders", |
||
| 92 | title: "Database error in order_items", |
||
| 93 | detail: err.message |
||
| 94 | } |
||
| 95 | }); |
||
| 96 | } else { |
||
| 97 | return res.status(201).json({ |
||
| 98 | data: { |
||
| 99 | message: "Products and orders have been copied" |
||
| 100 | } |
||
| 101 | }); |
||
| 102 | } |
||
| 103 | }); |
||
| 104 | } |
||
| 105 | }); |
||
| 106 | } |
||
| 107 | }); |
||
| 108 | } |
||
| 219 |